<?php
namespace Admin\Controller;

use Think\Controller;

// 商品
class ProductController extends Controller
{	
	//新增数据
	public function add()
	{
		$data = M('Product_class');
		$list = $data->order('cpid')->select();
		$this->assign('list',$list);

		$this->upload= U('upload');

		$this->display();
	}

	public function upload(){
    	$config = array(
		//文件上传的最大文件大小
			'maxSize'  => 3145728,      
		//文件上传保存的根路径
			'rootPath' => 'public/images/product/',  
		//文件上传的保存路径（相对于根路径）
			'savePath' => '',        
		//上传文件的保存规则
			'saveName' => array('uniqid', ''),  
		//允许上传的文件后缀
			'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
		//自动使用子目录保存上传文件
			'autoSub'  => false,      
		//子目录创建方式
			'subName'  => array('date', 'Ymd'),  
		);
    	$upload = new \Think\Upload($config);
    	$info = $upload->uploadOne($_FILES['file']);
    
	    if(!$info) {// 上传错误提示错误信息
	        $this->ajaxReturn(array('status'=>'ER','msg'=>$upload->getError()));
	    }else{// 上传成功
	    	$url = $info['savename'];
	        $this->ajaxReturn(array('status'=>'OK','url'=>$url));
	    }
    }

	public function insert()
	{	
		$active = M('Product'); //商品数据库
			$active->create();
		$id = $active->add();

		$data = M('Product_product_class'); // 商品与分类关联的数据库
		$shuzu = $_POST['product_class_cid'];
		foreach ($shuzu as $key => $value) {
			$live[] = array('product_id'=>$id,'product_class_cid'=>$value);
		}
		$result = $data->addAll($live);

		$iModel = M('Picture');
		foreach (I('post.photos') as $key => $value) {
    		$list[]=array('product_id'=>$id,'picture_url'=>$value);
    	}
    	$results = $iModel->addAll($list);
		
		if ($result) {
			$this->success('添加成功', 'view');
		} else {
			$this->error('添加失败');
		}
				
	} 

	//用表格的方式显示数据
	public function view()
	{
		$data = M('Product_product_class'); //实例化对象
		$cla = M('Product_class');
		// 查询满足要求的总记录数
		$count = $data->count();
		// 实例化分页类 传入总记录数和每页显示的记录数
		$Page = new \Think\Page($count, 20);
		
		$show = $Page->show(); //分页显示输出
		
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $data->join('RIGHT JOIN product ON product_product_class.product_id = product.id')->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();

		foreach ($list as $key => $value) {
			$list[$key]['class'] = $cla->where('cid='.$value['product_class_cid'])->find();
		}

		$this->assign('list', $list);
		$this->assign('page', $show); //赋值分页输出 		
		$this->display(); //输出模板

	}

	//修改商品数据
	public function save()
	{
		$id = $_GET['id'];

		$data = M('Product');
		$class = M('Product_class');
		$iModel = M('Picture');
	// 商品	
		$product = $data->find($id);
	// 分类
		$list = $class->order('cpid')->select();
	// 图片
		$image = $iModel->where('product_id='.$id)->select();
	
	// 商品与分类关联	
		$product_class = M('Product_product_class');
	// 查出已选分类
		$tmp = $product_class->where('product_id="'.$id.'"')->select();	
		foreach ($tmp as $key => $value) {
			$pcids[]=$value['product_class_cid'];
		}

		$this->assign('pcids',$pcids);
		$this->assign('image',$image);
		$this->assign('product',$product);
		$this->assign('list',$list);	
		$this->display();

	}

	public function amend()
	{
		$id = $_POST['id'];
		$active = M('Product');    	
	    $active->create();
	    $result = $active->save();

	    $data = M('Product_product_class'); // 商品与分类关联的数据表
	// 先删除分类
	    $result = $data->where('product_id="'.$id.'"')->delete();
	// 在添加分类	
		$shuzu = $_POST['product_class_cid'];
		foreach ($shuzu as $key => $value) {
			$live[] = array('product_id'=>$id,'product_class_cid'=>$value);
		}
		$result = $data->addAll($live);

		$iModel = M('Picture'); // 图片数据表
	// 先删除图片	
		$resultsd =	$iModel->where('product_id="'.$id.'"')->delete();
	// 在添加图片	
		foreach (I('post.photos') as $key => $value) {
    		$list[]=array('product_id'=>$id,'picture_url'=>$value);
    	}
    	$results = $iModel->addAll($list);
		
		if ($result) {
			$this->success('修改成功', 'view');
		} else {
			$this->error('修改失败');
		}
	}

	//删除数据
	public function cancel()
	{
		$id = $_GET['id'];
		$active = M('Product');
		$result = $active->where('id="'.$id.'"')->delete();
		if ($result) {
			$this->success('删除成功', 'view');
		} else {
			$this->error('删除失败');
		}
	}


	// 商品详情
	public function increase()
	{
		$id = $_GET['id'];
		$active = M('Product');
		$data = M('Product_class');

		$list = $active->join('product_product_class ON product.id = product_product_class.product_id')->where('id='.$id)->select();
		
		foreach ($list as $key => $value) {
			$list[$key]['class'] = $data->where('cid='.$value['product_class_cid'])->find();
			$value['img'] = $active->join('picture ON product.id = picture.product_id')->where('id='.$id)->select();

		}
		$list['img'] = $active->join('picture ON product.id = picture.product_id')->where('id='.$id)->select();

		$this->assign('value', $value);
		$this->assign('list', $list); 		
		$this->display();

	}


}
